package com.liu.stockmarket.repository.database.dao

import androidx.room.*
import com.liu.stockmarket.repository.database.entity.StockEntity
import com.liu.stockmarket.repository.database.entity.TransactionRecordEntity
import kotlinx.coroutines.flow.Flow
import java.math.BigDecimal

@Dao
interface StockDao {

    @Insert
    suspend fun save(stockEntity: StockEntity): Long

    @Update
    suspend fun update(stockEntity: StockEntity)

    @Query("select * from stock where id = :id limit 1")
    suspend fun queryById(id: Long): StockEntity?


    @Query("select * from stock where id = :id limit 1")
    fun queryByIdWithFlow(id: Long): Flow<StockEntity>?

    @Query(value = "select * from stock where code =:stockCode limit 2")
    suspend fun queryByStockCode(stockCode: String): List<StockEntity>

    /**
     * 股票、可转债
     */
    @Query(value = "select * from stock where ( stock_type=0 or stock_type =1) and self=0")
    fun queryAllStock(): Flow<List<StockEntity>>

    /**
     * 股票、可转债
     */
    @Query(value = "select * from stock where (holding_number != 0 and (stock_type=0 or stock_type =1)  and self=0) or (DATE(update_datetime) = CURRENT_DATE and (stock_type=0 or stock_type =1))")
    fun queryHoldingStock(): Flow<List<StockEntity>>


    /**
     * 股票、可转债
     */
    @Query(value = "select * from stock where holding_number = 0  and self=0 and (stock_type=0 or stock_type =1)  and DATE(update_datetime) != CURRENT_DATE")
    fun queryNoHoldingStock(): Flow<List<StockEntity>>


    @Query("select * from stock where stock_type =2 and self=0")
    fun queryAllFund():Flow<List<StockEntity>>

    /**
     * 基金
     */
    @Query(value = "select * from stock where (holding_number != 0 and stock_type=2)  and self=0  or (DATE(update_datetime) = CURRENT_DATE and stock_type=2)")
    fun queryHoldingFund(): Flow<List<StockEntity>>

    /**
     * 基金
     */
    @Query(value = "select * from stock where holding_number = 0 and self=0 and stock_type=2 and DATE(update_datetime) != CURRENT_DATE")
    fun queryNoHoldingFund(): Flow<List<StockEntity>>

    @Query(value = "select * from stock where self = 0")
    fun queryAllFlow(): Flow<List<StockEntity>>


    /**
     * 非自选
     */
    @Query(value = "select * from stock where self = 0")
    fun queryAllBuy(): List<StockEntity>

    /**
     * 所有
     */
    @Query(value = "select * from stock")
    fun queryAll(): List<StockEntity>

    /**
     * 删除股票
     */
    @Query(value = "delete from stock where id = :stockId")
    suspend fun deleteById(stockId: Long): Int

    @Query(value = "update stock set holding_price = :holdingPrice, holding_number=:holdingNumber where id=:stockId")
    suspend fun modifyHoldingPriceAndNumber(
        stockId: Long,
        holdingPrice: BigDecimal,
        holdingNumber: BigDecimal
    )

    @Insert
    suspend fun saveAll(stockEntitys: List<StockEntity>)

    @Query(value = "delete from stock")
    suspend fun deleteAll()

    /**
     * 先删除数据，后更新
     */
    @Transaction
    suspend fun deleteAllForSaveAll(stockEntitys: List<StockEntity>) {
        deleteAll()
        saveAll(stockEntitys)
    }

    /**
     * 查询自选
     */
    @Query("select * from stock where self = 1")
    fun querySelf():Flow<List<StockEntity>>
}